package gold.digger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC13 {
    public int romanToInt(String s) {
        if (null == s || s.length() == 0) {
            return 0;
        }

        /**
         * Symbol       Value
         I             1
         size             5
         X             10
         L             50
         C             100
         D             500
         M             1000
         */
        Map<Character, Integer> romanDict = new HashMap<Character, Integer>();
        romanDict.put('I', 1);
        romanDict.put('V', 5);
        romanDict.put('X', 10);
        romanDict.put('L', 50);
        romanDict.put('C', 100);
        romanDict.put('D', 500);
        romanDict.put('M', 1000);

        if (s.length() == 1) {
            return romanDict.get(s.charAt(0));
        }

        int result = 0;
        int i = 0, j = 1;
        for (; i < s.length(); ) {
            int curNum = romanDict.get(s.charAt(i));
            int nextNum = j < s.length() ? romanDict.get(s.charAt(j)) : 0;
            if (curNum < nextNum) {
                result += nextNum - curNum;
                i = j + 1;
                j += 2;
            } else {
                result += curNum;
                i += 1;
                j += 1;
            }
        }

        return result;
    }

    public static void main(String[] args) {
        LC13 an = new LC13();
        /**
         "aaabaaaababcbccbaab"
         "c*c*.*c*a*..*c*"
         */
        System.out.println(an.romanToInt("MCMXCIV"));
    }
}
